<?php

class GcmCore extends Controller {
    
    
    const SERVER_KEY = 'AIzaSyCaHSOBwdT3PT7Hx1MBM_j9EhplDK5klLY';
    const GCM_SERVER = 'https://android.googleapis.com/gcm/send';

    /**
     * sendGCM
     *
     * Send messages to Google Cloud Messaging Server
     *
     * @param int $user_id
     * @param string $message
     *
     * @return mixed
     */
    public static function sendGCM($gcmIDs, $message = "New interactions") {

        if ($gcmIDs) {
            $headers = array(
                'Content-Type:application/json',
                'Authorization:key=' . GcmCore::SERVER_KEY
            );

            $data = array(
                'registration_ids' => $gcmIDs,
                'data' => array('message' => $message)
            );

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, GcmCore::GCM_SERVER);
            if ($headers)
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

            $response = curl_exec($ch);

            curl_close($ch);
            return($response);
        } else {
            return -1;
        }
    }
}